﻿using System;
using System.Web;
//using System.Data.SqlClient;
using System.Runtime.Serialization;


namespace Sharp
{
    [DataContract]
    public class Web
    {
       // public HttpRequest Request { get; set; }
       // public HttpResponse Response { get; set; }
        [IgnoreDataMember]
        public HttpContext context { get; set; }
        public FormWrapper Form { get { return new FormWrapper() { context = context }; } }
        public FormArrayWrapper FormArray { get { return new FormArrayWrapper() { context = context }; } }
        public QueryStringWrapper Query { get { return new QueryStringWrapper() { context = context }; }   }
        
        [DataMember]
        public StateBag sb { get; set; }
        public String[] UrlParts { get { return context.Request.UrlParts(); } }
        public Boolean isPost { get { return context.Request.RequestType.Like("POST"); } }



        public Web()
        {
            
            context = HttpContext.Current; 
          //  Request = context.Request;
          //  Response = context.Response;
            sb = new StateBag();
        }

        public Web(HttpContext _context)
        {
            context = _context; 
            //  Request = context.Request;
            //  Response = context.Response;
            sb = new StateBag();
        }
        public class FormArrayWrapper
        {
            private HttpContext _context { get { return context ?? HttpContext.Current; } }
            public HttpContext context { get; set; }

            public String[] this[String index]
            {
                get
                {
                    if (_context.Request.Form[index] != null)
                        return _context.Request.Form.GetValues(index);
                    else
                        return new string[] { };
                }

            }
        }
        public class FormWrapper
        {
            private HttpContext _context { get { return context ?? HttpContext.Current; }   }
            public HttpContext context {get;set;}
            
            public String this[String index]
            {
                get { return _context.Request.Form[index] ?? ""; }

            }
        }
        public class QueryStringWrapper
        {
            private HttpContext _context { get { return context ?? HttpContext.Current; }  }
            public HttpContext context {get;set;}

            public String this[String index]
            {
                get { return _context.Request.QueryString[index] ?? ""; }

            }
        }

        public void Write(object Txt)
        {
            if (Txt != null)
                context.Response.Write(Txt.ToString());

            //try { if (context.Session["count"] != null)
            //context.Response.Write(context.Session["count"]);
            //} catch (Exception e) {}
        }



    }
}
